﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Hue_Demo_PCL.ViewModels;
using System;
using Hue_Demo_PCL.Models;
using System.Diagnostics;
#if NETFX_CORE
using Windows.UI.Xaml;
#elif WINDOWS_PHONE
using System.Windows.Threading;

#endif

namespace Hue_Demo_Phone.ViewModels
{
    public class HueClientAnimateViewModel : HueAnimationViewModel
    {
        private DispatcherTimer _animationTimer;
        private DateTime _startTime;

        /// <summary>
        /// Initializes a new instance of the HueAnimationViewModel class.
        /// </summary>
        public HueClientAnimateViewModel()
        {

            _animationTimer = new DispatcherTimer();
            _animationTimer.Interval = new System.TimeSpan(0, 0, 0, 0, 250);
            
#if NETFX_CORE
            _animationTimer.Tick += _animationTimer_WinTick;
#elif WINDOWS_PHONE
            _animationTimer.Tick += _animationTimer_WPTick;
#endif
            CloseEncounter();
        }

        void _animationTimer_WinTick(object sender, object e)
        {
            ProcessAnimationFrame();
        }

        void _animationTimer_WPTick(object sender, System.EventArgs e)
        {
            ProcessAnimationFrame();
        }

        private void ProcessAnimationFrame()
        {
            // Figure out the time since start
            TimeSpan animationTime = DateTime.Now - _startTime;
            bool isAnimationComplete = true;
            // Check to see if it is time to run the next animation
            foreach (HueKeyFrame hkf in HueAnimation)
            {
                if (!hkf.IsComplete)
                {
                    if (hkf.ChangeTime < animationTime)
                    {
                        hkf.UpdateHueSat();
                        UpdateLight(hkf as HueLight);
                        Debug.WriteLine("Light Change: " + hkf.Index.ToString());
                        hkf.IsComplete = true;
                    }
                    isAnimationComplete = false;
                }
            }
            if (isAnimationComplete)
            {
                IsAnimating = false;
                _animationTimer.Stop();
                ResetAnimation();
            }   
        }

        private void ResetAnimation()
        {
            foreach (HueKeyFrame hkf in HueAnimation)
                hkf.IsComplete = false;
        }        

        #region AnimateLights Command

        private RelayCommand _animateLights;

        /// <summary>
        /// Gets the AnimateLights.
        /// </summary>
        public RelayCommand AnimateLights
        {
            get
            {
                return _animateLights
                    ?? (_animateLights = new RelayCommand(
                    () =>
                    {
                        if (IsAnimating)
                            return;
                        _startTime = DateTime.Now;
                        _animationTimer.Start();

                    }, () => !IsAnimating));
            }
        }

        #endregion

        #region Pre-Programmed Light Changes
        public void SetCloseEncounters() 
        {
            CloseEncounter();
        }
        private void CloseEncounter(){
            HueAnimation.Clear();
           HueKeyFrame h1 = new HueKeyFrame(){
                Red = 250,
                Green = 0,
                Blue = 0,
                Brightness = 250,
                Index =1,
                ChangeTime = new TimeSpan(0,0,0,0,100), 
                IsOn = true                            
                };

           // Key Frame 2
           HueKeyFrame h2 = new HueKeyFrame(){
                Red = 0,
                Green = 250,
                Blue = 0,
                Brightness = 250,
                Index = 2,
                ChangeTime = new TimeSpan(0,0,0,1,200), 
                IsOn = true                            
                };

           HueKeyFrame h2_1 = new HueKeyFrame()
           {
               Red = 250,
               Green = 0,
               Blue = 0,
               Brightness = 20,
               Index = 1,
               ChangeTime = new TimeSpan(0, 0, 0, 1, 200),
               IsOn = true
           };
           
           // Key Frame 3
           HueKeyFrame h3 = new HueKeyFrame(){
                Red = 250,
                Green = 0,
                Blue = 250,
                Brightness = 250,
                Index =3,
                ChangeTime = new TimeSpan(0,0,0,2,300), 
                IsOn = true                            
                };
           HueKeyFrame h3_1 = new HueKeyFrame()
           {
               Red = 0,
               Green = 250,
               Blue = 0,
               Brightness = 20,
               Index = 2,
               ChangeTime = new TimeSpan(0, 0, 0, 2, 300),
               IsOn = true
           };

            //KeyFrame 4
           HueKeyFrame h4 = new HueKeyFrame(){
                Red = 250,
                Green = 250,
                Blue = 0,
                Brightness = 250,
                Index =1,
                ChangeTime = new TimeSpan(0,0,0,3,500), 
                IsOn = true                            
                };

           HueKeyFrame h4_1 = new HueKeyFrame()
           {
               Red = 250,
               Green = 0,
               Blue = 250,
               Brightness = 20,
               Index = 3,
               ChangeTime = new TimeSpan(0, 0, 0, 3, 500),
               IsOn = true
           };

            // Key Frame 5

           HueKeyFrame h5 = new HueKeyFrame(){
                Red = 0,
                Green = 0,
                Blue = 250,
                Brightness = 250,
                Index = 2,
                ChangeTime = new TimeSpan(0,0,0,4,800), 
                IsOn = true                            
                };

           HueKeyFrame h6 = new HueKeyFrame()
           {
               Red = 250,
               Green = 250,
               Blue = 0,
               Brightness = 20,
               Index = 1,
               ChangeTime = new TimeSpan(0, 0, 0, 4, 800),
               IsOn = true
           };
           
           HueKeyFrame h7 = new HueKeyFrame(){
                Red = 0,
                Green = 0,
                Blue = 250,
                Brightness = 20,
                Index =2,
                ChangeTime = new TimeSpan(0,0,0,8,200), 
                IsOn = true                            
                };

           HueAnimation.Add(h1);
           HueAnimation.Add(h2);
           HueAnimation.Add(h3);
           HueAnimation.Add(h4);
           HueAnimation.Add(h5);
           HueAnimation.Add(h6);
           HueAnimation.Add(h7);
           HueAnimation.Add(h2_1);
           HueAnimation.Add(h3_1);
           HueAnimation.Add(h4_1);
       }

        public void RunReset()
        {
            Reset();
            _startTime = DateTime.Now;
            _animationTimer.Start();
        }

        private void Reset()
        {
            HueAnimation.Clear();
            HueKeyFrame h1 = new HueKeyFrame()
            {
                Red = 0,
                Green = 0,
                Blue = 0,
                Brightness = 10,
                Index = 1,
                ChangeTime = new TimeSpan(0, 0, 0, 0, 100),
                IsOn = true
            };

            // Key Frame 2
            HueKeyFrame h2 = new HueKeyFrame()
            {
                Red = 0,
                Green = 00,
                Blue = 0,
                Brightness = 10,
                Index = 2,
                ChangeTime = new TimeSpan(0, 0, 0, 0, 100),
                IsOn = true
            };

            HueKeyFrame h3 = new HueKeyFrame()
            {
                Red = 0,
                Green = 0,
                Blue = 0,
                Brightness = 10,
                Index = 3,
                ChangeTime = new TimeSpan(0, 0, 0, 0, 100),
                IsOn = true
            };
            HueAnimation.Add(h1);
            HueAnimation.Add(h2);
            HueAnimation.Add(h3);
        }
 
        //.5, 1.6, 2.7, 3.8, 5.2, end 7.6
        #endregion
    }
}